package com.qf.web.servlet;

import com.qf.pojo.TbUser;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;
import com.qf.utils.StringConstans;
import com.qf.utils.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;

/**
 * Create by 周志豪 on 2022/8/16  19:57
 *
 * @authoe 周志豪
 * @veision 1.0
 * //TODO  想好了再去做
 * @since 1.0
 */

public class UserServlet {
    private UserService userService = new UserServiceImpl();
    //登陆
    public String register(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            String email = req.getParameter("email");
            String gender = req.getParameter("gender");
            gender = new String(gender.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
            if (StringUtils.isEmpty(username) || !StringUtils.isLenghtEnough(username, 6)) {
                req.setAttribute("exp", "请输入不少于6位的账号名称");
                return StringConstans.FORWARD + "/error/error.jsp";
            }
            if (StringUtils.isEmpty(password) || !StringUtils.isLenghtEnough(password, 6)) {
                req.setAttribute("exp", "请输入不少于6位的密码");
                return StringConstans.FORWARD + "/error/error.jsp";
            }
            if (!email.matches(StringConstans.EMAILREGE)) {
                req.setAttribute("exp", "请输入正确的邮箱");
                return StringConstans.FORWARD + "/error/error.jsp";
            }
            System.err.println("数据校验没问题");
            //插入到数据库
            TbUser tbUser = new TbUser();
            tbUser.setPassword(password);
            tbUser.setEmail(email);
            tbUser.setUsername(username);
            tbUser.setGender(gender);
            userService.addUser(tbUser);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return StringConstans.REDIRECT + "/registerSuccess.jsp";
    }

    public String login(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String code = request.getParameter("code");
        if (StringUtils.isEmpty(code)) {
            request.setAttribute("exp","验证码为空");
            return StringConstans.FORWARD + "/error/error.jsp";
        }
        String codeSave = (String) request.getSession().getAttribute("code");
        request.getSession().removeAttribute("code");
        if (!code.equalsIgnoreCase(codeSave)) {
            request.setAttribute("exp","验证码错误");
            return StringConstans.FORWARD + "/error/error.jsp";
        }
        TbUser user = userService.login(username, password);
        if (user != null) {
            if (user.getFlag() == 0) {
            }
            request.getSession().setAttribute("user", user);
            CompletableFuture.runAsync(()->{
                userService.updateLoginTime(user.getId());
            });
            return StringConstans.REDIRECT + "/index.jsp";
        }else{
            return StringConstans.REDIRECT + "/login.jsp";
        }

    }
}
